<?php
class Slog_Router
{
	static protected $_instance;
	
	public $mode;
	public $controller;
	public $action;
	public $params;

	static public function getInstance()
	{
		return self::$_instance ? self::$_instance : (self::$_instance = new self());
	}
	
	public function init()
	{
		// Obtenemos la URI de la aplicación
		switch (config()->get('router.parser', 'q')) {
			case 'q':
				$appuri = $_GET['q'];
				break;
				
			case 'filename':
				$filename = basename($_SERVER['SCRIPT_FILENAME']);
				$appuri = substr($_SERVER['REQUEST_URI'], strlen($filename));
				// quitamos URI base
				$appuri = substr($appuri, strlen(SLOGGY_URI));
				break;
				
			case 'uri':
				$appuri = $_SERVER['REQUEST_URI'];
				// quitamos URI base
				$appuri = substr($appuri, strlen(SLOGGY_URI));
				break;
		}
		$appuri = explode('/', trim($appuri, '/'));
		
		// Token de private
		$private_token = config()->get('router.private.token');
		
		// Obtenemos visibilidad
		if ($appuri[0] == $private_token) {
			array_shift($appuri);
			$mode = 'private';
		} else {
			$mode = 'public';
		}
		
		// Controlador
		$controller = array_shift($appuri);
		if (!$controller) $controller = 'index';
		
		// Acción
		$action = array_shift($appuri);
		if (!$action) $action = 'index';
		
		$this->mode = $mode;
		$this->controller = $controller;
		$this->action = $action;
		$this->params = $appuri;
	}

}


function router()
{
	return Slog_Router::getInstance();
}
